<?php

namespace App\Models;

use App\Http\Controllers\Client\Talent\TalentApplyController;
use App\Models\Common\Common;
use App\Models\Tools\AliOss;
use App\Models\Tools\Data;
use EasyWeChat\Kernel\Http\StreamResponse;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Redis;

class Talent
{

    /**
     * 查询达人信息
     * @param $we_id
     * @return array
     */
    public static function getTalentMessage($we_id)
    {
        // 查询当前账号
        $talent = DB::table('talent')->where('we_id','=',$we_id)->first();
        $talent = json_decode(json_encode($talent), true);
        if($talent){
            $channels = Common::getChannels(); // 查询所有达人等级
            foreach($channels as $channel){
                if($talent['channel_id'] === $channel['channel_id']){
                    $talent['channel_name'] = $channel['channel_name'];
                    break;
                }
            }
            $talent['short_name'] = DB::table('dict_area')->where('id','=',$talent['district'])->value('short_name');
            $talent['status_name'] = Data::$talentStatus[$talent['status']];
            $talent['verify_img'] = AliOss::getUrl($talent['verify_img']);
            $talent['collection_qr_code'] = AliOss::getUrl($talent['collection_qr_code']);
        }
        return $talent;
    }


    /**
     * 查询达人日志
     * @param int $talent_id talent_id
     * @return array
     */
    public static function talentLog(int $talent_id): array
    {
        $logs = DB::table('talent_log as tl')
            ->leftJoin('lottery_users as lu','tl.we_id','=','lu.we_id')
            ->leftJoin('users','tl.user_id','=','users.id')
            ->where('tl.talent_id', '=', $talent_id)
            ->orderByDesc('tl.log_id')
            ->select('tl.active_type', 'tl.created_at','tl.we_id','tl.user_id','lu.nickname','users.name')
            ->get()
            ->map(function ($item) {
                if ($item->nickname) {
                    $name = '用户【' . $item->nickname . '】: ';
                }
                if ($item->name) {
                    $name = '管理员【' . $item->name . '】: ';
                }
                $item->name = $name?? '';
                $item->action = Data::$talentAction[$item->active_type];
                $item->remark = $item->name . $item->action;
                return $item;
            })
            ->toArray();
        $logs = json_decode(json_encode($logs), true);
        return $logs;
    }

}
